package cn.chyn;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebhookSender {
    public static void main(String[] args) {
        try {
            // Webhook URL
            String url = "https://c2f8-14-145-41-115.ngrok-free.app/chyn-webhook";

            // JSON 数据
            String jsonData = "{"
                    + "\"ref\": \"refs/heads/master\","
                    + "\"repository\": {"
                    + "\"name\": \"chyn-backend\""
                    + "}"
                    + "}";

            // 创建 URL 对象
            URL obj = new URL(url);

            // 打开连接
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // 设置请求方法
            con.setRequestMethod("POST");

            // 设置请求头
            con.setRequestProperty("X-Gitee-Event", "Push Hook");
            con.setRequestProperty("Content-Type", "application/json");

            // 启用输入输出流
            con.setDoOutput(true);

            // 发送 POST 请求
            try (OutputStream os = con.getOutputStream()) {
                byte[] input = jsonData.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 获取响应码
            int responseCode = con.getResponseCode();
            System.out.println("HTTP Response Code: " + responseCode);

            // 处理响应（可选）
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("请求成功");
            } else {
                System.out.println("请求失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
